package com.boarsoft.flow.event;

import java.util.LinkedList;
import java.util.List;

import com.boarsoft.flow.core.SimpleFlow;
import com.boarsoft.flow.core.bean.BaseNode;

/**
 * 支持多个listeners
 * 
 * @author Mac_J
 *
 */
public class SimpleFlowListenerImpl implements SimpleFlowListener {
	protected List<SimpleFlowListener> listeners = new LinkedList<>();

	@Override
	public void onBegin(SimpleFlow flow) {
		for (SimpleFlowListener l : listeners) {
			l.onBegin(flow);
		}
	}

	@Override
	public void onEnd(SimpleFlow flow) {
		for (SimpleFlowListener l : listeners) {
			l.onEnd(flow);
		}
	}

	@Override
	public void onError(SimpleFlow flow) {
		for (SimpleFlowListener l : listeners) {
			l.onError(flow);
		}
	}

	@Override
	public void onSuspend(SimpleFlow flow) {
		for (SimpleFlowListener l : listeners) {
			l.onSuspend(flow);
		}
	}

	@Override
	public void onStop(SimpleFlow flow) {
		for (SimpleFlowListener l : listeners) {
			l.onStop(flow);
		}
	}

	@Override
	public void onTimeout(SimpleFlow flow) {
		for (SimpleFlowListener l : listeners) {
			l.onTimeout(flow);
		}
	}

	@Override
	public void onBeforeStep(SimpleFlow flow, Object data, BaseNode curr) {
		for (SimpleFlowListener l : listeners) {
			l.onBeforeStep(flow, data, curr);
		}
	}

	@Override
	public void onAfterStep(SimpleFlow flow, Object data, Throwable flowEx, String next, Throwable stepEx, long time) {
		for (SimpleFlowListener l : listeners) {
			l.onAfterStep(flow, data, flowEx, next, stepEx, time);
		}
	}

	@Override
	public void onBeforeHandle(SimpleFlow flow, Object data, Throwable throwable, Object dd) {
		for (SimpleFlowListener l : listeners) {
			l.onBeforeHandle(flow, data, throwable, dd);
		}
	}

	@Override
	public void onAfterHandle(SimpleFlow flow, Object data, Throwable throwable, Object checkInData, Object dataCheckOut,
			Throwable handleEx) {
		for (SimpleFlowListener l : listeners) {
			l.onAfterHandle(flow, data, throwable, checkInData, dataCheckOut, handleEx);
		}
	}

	@Override
	public void onFailHandle(SimpleFlow flow, Object data, Throwable throwable, Object checkInData, Throwable handleEx) {
		for (SimpleFlowListener l : listeners) {
			l.onFailHandle(flow, data, throwable, checkInData, handleEx);
		}
	}

	@Override
	public void onFailStep(SimpleFlow flow, Object data, Throwable flowEx, String step, Throwable stepEx) {
		for (SimpleFlowListener l : listeners) {
			l.onFailStep(flow, data, flowEx, step, stepEx);
		}
	}
	
	@Override
	public void onFinally(SimpleFlow flow) {
		for (SimpleFlowListener l : listeners) {
			l.onFinally(flow);
		}
	}
	
	public List<SimpleFlowListener> getListeners() {
		return listeners;
	}
	
	public void setListeners(List<SimpleFlowListener> listeners) {
		this.listeners = listeners;
	}

	@Override
	public void onAfterBranch(SimpleFlow flow) {
		// Nothing TO DO
	}

}
